import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MyFilter implements Filter{

	@Override
	public void init(FilterConfig paramFilterConfig) throws ServletException {
		
		System.out.println("IN init methode"+paramFilterConfig );
	}

	@Override
	public void doFilter(ServletRequest paramServletRequest,
			ServletResponse paramServletResponse, FilterChain paramFilterChain)
			throws IOException, ServletException {
		System.out.println("IN doFilter methode");
		
		HttpServletRequest request=(HttpServletRequest)paramServletRequest;
       HttpServletResponse response=(HttpServletResponse)paramServletResponse;
		 System.out.println(request.getRequestURI());
		 System.out.println(request.getRequestURL());
		 if(!request.getRequestURI().contains("Login") && !request.getRequestURI().contains("login.html") ) {
			 HttpSession session=	request.getSession();
			 if(null==session.getAttribute("username")){
				 response.sendRedirect("login.html");
			 }
		 }
		//pass to another filter if exist. otherwise   actual resource will be accessed. 
		paramFilterChain.doFilter(paramServletRequest, paramServletResponse);
		
		System.out.println("after doFilter");
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("In Destroy ");
		
	}
	
	
	
	

}
